// miniprogram/pages/NotifyRecord/NotifyRecord.js
const { $Message } = require('./../../dist/base/index');
import { $stopWuxRefresher, $stopWuxLoader } from '../../dist/index'
import sendRequest from "../../util/sendRequest"
const { $Toast } = require('../../dist/base/index');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //侧边框显示值
    showLeft: false,
    currentChoice:['全部教员'],
    currentChoiceId:[],
    current:1,
    pageSize:15,
    notifyrecorddata:[],
    //老师列表
    teacherList: [],
    teacherNameList: []

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    $Toast({
      content: '加载中',
      type: 'loading',
      duration:0
    });
    this.getData(true);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var windowWidth = wx.getSystemInfoSync().windowWidth;
    var windowHeight = wx.getSystemInfoSync().windowHeight;
    //rpx与px单位之间的换算 : 750/windowWidth = 屏幕的高度（rpx）/windowHeight
    // var scroll_height = 750 * windowHeight / windowWidth - 150;
    this.setData({
      scroll_height: windowHeight -48
    })


    // //获取老师数据列表
    this.getTeacherList();

    this.setData({
      nowTime: new Date().getTime(),
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 侧边框关闭事件
   */
  toggleLeft(){
    this.setData({
      showLeft:false
    })
  },
  //发送通知单击事件--显示侧边框
  sendMessage(){
    this.setData({
      showLeft: true
    })
  },
  //取消按钮单击事件--隐藏侧边框
  cancelBtn(){
    this.setData({
      showLeft: false
    })
  },
  getUserInfo(info) {
    const userInfo = info.detail.userInfo
    this.setData({
      userInfo,
      hasUserInfo: true
    })
  },
  //发送通知信息表单提交事件
  sendBtn(){
    if(this.data.formIdList.length >= this.data.formIdNumber){
      this.data.formIdList = []
    }
    console.log('submitForm',e);
    this.data.formIdList.push(e.detail.detail.formId);
    console.log("formIdList",this.data.formIdList)
    if(this.data.formIdList.length != this.data.formIdNumber){
      return;
    }

    console.log("formIdList",this.data.formIdList)
    console.log("currentChoiceId",this.data.currentChoiceId)


    wx.cloud.callFunction({
      name: "sendMessage",
      data: {
        formIdList: this.data.formIdList,
        touserList: this.data.currentChoiceId
      },
      success(res) {
        console.log('submit form success', res)
      },
      fail: err => {
        console.log('submit form fail, errMsg is:', err)
      }
    })
    
  },
  handleFruitChange({ detail = {} }) {
      if(detail && detail.current && detail.value==='全部教员'){
        this.data.currentChoice = ['全部教员'];
      }else{
        const index = this.data.currentChoice.indexOf(detail.value);
        const listIndex = this.data.teacherNameList.indexOf(detail.value);
        index === -1 ? this.data.currentChoice.push(detail.value) : this.data.currentChoice.splice(index, 1);
        index === -1 ? this.data.currentChoiceId.push(this.data.teacherList[listIndex].id) : this.data.currentChoiceId.splice(index, 1);
      }
      this.setData({
        currentChoice: this.data.currentChoice
      });
  },
  //获取老师列表
  getTeacherList(){

    sendRequest({
      url:'/api/user/getValidTeacher',
      success: res => {
        
        if(res && res.data.code ===200){
          for (let i in res.data.data) {
            
            this.data.teacherList.push({ id: res.data.data[i].id, actualname: res.data.data[i].actualname })
            this.data.teacherNameList.push(res.data.data[i].actualname);
          }
          this.setData({
            teacherList: this.data.teacherList,
            teacherNameList: this.data.teacherNameList
          })
          // this.data.currentChoice
        }
      }
    })

  
  },
  //获取发送记录数据
  getData(flag){
    if(flag){
      this.data.current = 1;
    }else{
      this.data.current ++;
    }
    sendRequest({
      url: "/api/notifyrecord/queryNotifyrecord",
      data: {
        current: this.data.current,
        pageSize: this.data.pageSize
      },
      success: res => {
        if (res && res.data) {
          if (flag){
            this.data.notifyrecorddata = res.data.data.records
          }else{
            this.data.notifyrecorddata = this.data.notifyrecorddata.concat(res.data.data.records)
          }

          this.setData({
            notifyrecorddata: this.data.notifyrecorddata
          },()=>{
            if (flag){
              $stopWuxRefresher()
            }else{
              if(this.data.current !== res.data.data.pages){
                //单次数据加载完成
                $stopWuxLoader()
              }else{
                //所有数据加载完成
                $stopWuxLoader('#wux-refresher', this, true)
              }
            }
          })
        }
      },
      fail: err => {
        $Toast({
          content: '加载失败',
          type: 'error'
        });
      },
      complete:()=>{
        $Toast.hide()
      }
    })
  },
  //下拉完成事件
  onRefresh(){
    this.getData(true);
  },
  onLoadmore(){
    this.getData(false);
  },
  viewscroll(e){
    this.setData({
      scrollTop: e.detail.scrollTop + 10
    })
  }
})